import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:inmotion_pro/l10n/local.dart';
import 'package:inmotion_pro/pages/others/custom_view/setting_items/setting_leading.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'package:safemap/safemap.dart';

import '../../config/r.dart';

// 第三方账号绑定状态枚举
enum Account3rdBindStatus {
  unbound(0),
  bound(1);

  String get description {
    switch (this) {
      case Account3rdBindStatus.unbound:
        return local.unbind;
      case Account3rdBindStatus.bound:
        return local.bind;
    }
  }

  final int rawValue;
  const Account3rdBindStatus(this.rawValue);
}

// 第三方账号模型
class AccountModel3rd extends BaseData {
  Auth3rdType? socialType;
  String platform;
  String name;
  String logoUrl;
  Account3rdBindStatus bindStatus;

  AccountModel3rd({
    this.platform = "",
    this.name = "",
    this.logoUrl = "",
    this.bindStatus = Account3rdBindStatus.unbound,
    this.socialType,
  });

  factory AccountModel3rd.fromJson(Map<String, dynamic> json) {
    SafeMap safeMap = SafeMap(json);
    return AccountModel3rd(
      platform: safeMap["platform"].string ?? "",
      name: safeMap["name"].string ?? "",
      logoUrl: safeMap["logo"].string ?? "",
      bindStatus: Account3rdBindStatus.values.firstWhere(
          (e) => e.rawValue == (safeMap["state"].toInt ?? 0),
          orElse: () => Account3rdBindStatus.unbound),
    );
  }

  // 获取对应的图标
  Widget get leading {
    if (socialType != null) {
      switch (socialType!) {
        case Auth3rdType.apple:
          return const SettingLeading(imgPath: R.appleLoginIcon, size: 20);
        case Auth3rdType.google:
          return const SettingLeading(imgPath: R.googleLoginIcon, size: 20);
      }
    } else {
      return const SizedBox.shrink();
    }
  }

  Map<String, dynamic> toMap() {
    return {
      "platform": platform,
      "name": name,
      "logo": logoUrl,
      "state": bindStatus.rawValue,
    };
  }
}
